<template>
  <a-modal  :maskClosable="false"
    v-if="visible"
    :width="500"
    @ok="editPermissions()"
    :confirm-loading="confirmLoading"
    title="权限编辑"
    v-model="visible"
  >
    <MenuTree2 v-model="dataForm.permissionIds"></MenuTree2>
  </a-modal>


</template>

<script>
  import MenuTree2 from '@/hzjComponents/treeSelect/MenuTree2'

  export default {
    inject: ['upShow'],
    components: { MenuTree2 },
    data() {
      return {
        visible: false,
        confirmLoading:false,
        dataForm: {
          id: null,
          permissionIds: []
        },

        /*   -------弹窗--------*/
        mdl: {
          labelCol: {
            xs: { span: 24 },
            sm: { span: 5 }
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 }
          }
        }
      }
    },
    methods: {
      init(id, row) {
        if (id !== null) {
          this.dataForm.id = row.id
          if (row.permissionIds) {
            this.dataForm.permissionIds = row.permissionIds
          } else {
            this.dataForm.permissionIds = []
          }

        } else {
          this.dataForm = {
            id: null,
            permissionIds: []
          }
        }
        this.visible = true
      },

      editPermissions() {
        this.confirmLoading = true
        //编辑权限
        const params = this.dataForm.permissionIds.map(permissionId => {
          return {
            roleId: this.dataForm.id,
            permissionId: permissionId,
            permissionType: 1
          }
        })
        this.$axios
          .post(this.$URLMAP.url.role.perms.set, params)
          .then((response) => {
            if (response.code === 200) {
              this.upShow(1, '权限设置成功')
              this.confirmLoading = false
              this.visible = false
              this.$emit('refreshDataList' )
            } else {
              this.upShow(2, res.msg || '权限设置失败')
              this.confirmLoading = false
            }
          }).catch(err => {
          this.upShow(2, '设置权限接口调用失败')
          this.confirmLoading = false
        })
      }
    },
    watch: {
      checkedKeys(val) {
        console.log('onCheck', val)
      }
    }
  }
</script>
